package com.dk.category.controller;

import com.dk.category.service.CategoryHotJob;
import com.dk.feign.api.Category2AdminService;
import com.dk.category.constants.CategoryConstants;
import com.dk.category.constants.RedisConstants;
import com.dk.category.dto.CategoryDTO;
import com.dk.category.service.CategoryService;
import com.dk.category.service.RedisService;
import com.ts.common.core.constant.Constant;
import com.ts.common.core.page.PageData;
import com.ts.common.core.utils.Result;
import com.ts.common.core.validator.AssertUtils;
import com.ts.common.core.validator.ValidatorUtils;
import com.ts.common.core.validator.group.AddGroup;
import com.ts.common.core.validator.group.DefaultGroup;
import com.ts.common.core.validator.group.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 
 *
 * @author toString toString@qq.com
 * @since 1.0.0 2023-12-04
 */
@Slf4j
@RestController
@RequestMapping("/category")
@Api(tags="")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisService redisService;

    @Autowired
    private CategoryHotJob categoryHotJob;

    @Autowired
    private Category2AdminService category2AdminService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<CategoryDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<CategoryDTO> page = categoryService.page(params);

        return new Result<PageData<CategoryDTO>>().ok(page);
    }

    /**
     *更多圈子
     * @return
     */
    @PostMapping("groupList")
    public Result<Map<String, Object>> list(@RequestBody Map<String, Object> params) {
        // 封装
        Map<String, Object> mapList = new HashMap<>();
        mapList.put("hotCircleList", hotCircle());
        mapList.put("allCircleList", allCircleList(params));
        return new Result<Map<String, Object>>().ok(mapList);
    }

    /**
     * 查询热门圈子
     */
    @GetMapping("hotCircle")
    public List<CategoryDTO>  hotCircle() {
        return redisService.getHotCircleList();
    }

    /**
     * 查询全部圈子
     */
    @GetMapping("allCircleList")
    public List<CategoryDTO>  allCircleList(Map<String, Object> params) {
        // 查列表
        log.info("请求参数：{}", params);
        return redisService.getCircleList(params);
    }

    @GetMapping("getById")
    @ApiOperation("信息")
    public Result<CategoryDTO> get(@RequestParam("id") Integer id){
        CategoryDTO data = categoryService.get(id.longValue());
        return new Result<CategoryDTO>().ok(data);
    }

    @PostMapping("save")
    @ApiOperation("保存")
    public Result save(@RequestBody CategoryDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        categoryService.save(dto);

        // 刷新redis缓存
        redisService.refreshRedisCache();

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    public Result update(@RequestBody CategoryDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        categoryService.update(dto);

        // 刷新redis缓存
        redisService.refreshRedisCache();

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        categoryService.delete(ids);

        // 刷新redis缓存
        redisService.refreshRedisCache();

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<CategoryDTO> list = categoryService.list(params);

//        ExcelUtils.exportExcelToTarget(response, null, list, CategoryExcel.class);
    }

    @GetMapping("test")
    public String testOpenFeign(){
        return category2AdminService.getCircleHotNum(RedisConstants.CIRCLE_HOT_NUM_CODE);
    }

    @GetMapping("calculateHot")
    public void calculateCircleHot(@RequestParam Integer hotCircleNum){
        categoryHotJob.calculateCircleHot(hotCircleNum);
    }
}